package com.study.orderserviceprovider.costomPropertySource;

import org.springframework.boot.json.JsonParser;
import org.springframework.boot.json.JsonParserFactory;
import org.springframework.cloud.bootstrap.config.PropertySourceLocator;
import org.springframework.core.env.Environment;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description：
 * @author: 李军容
 * @version: 2020/12/2 19:39
 */
public class CustomJsonPropertiesLocator implements PropertySourceLocator {

    private final String location = "classpath:custom.json";

    private final ResourceLoader resourceLoader = new DefaultResourceLoader(getClass().getClassLoader());

    @Override
    public PropertySource<?> locate(Environment environment) {
        return new CustomPropertySource("jsonPropertyConfig", getPropertyMap());
    }

    private Map<String, Object> getPropertyMap() {
       String fileContext = getFileContext();
       if(null == fileContext){
           return  null;
       }
       JsonParser jsonParser = JsonParserFactory.getJsonParser();
       Map<String,Object>  fileMap = jsonParser.parseMap(fileContext);
       Map<String,Object> enviromentMap = new HashMap<String,Object>();
       parseJsonMap("",fileMap,enviromentMap);
       return enviromentMap;
    }

    private void parseJsonMap(String prefix, Map<String, Object> fileMap,
                                             Map<String, Object> result) {
        if(prefix.length() > 0){
            prefix += ".";
        }

        for(String key : fileMap.keySet()){
            Object value = fileMap.get(key);
            prefix += key;
            if(value instanceof Map){
                parseJsonMap(prefix, (Map<String, Object>) value,result);
            }else{
                result.put(prefix,value);
            }
        }
    }

    private String getFileContext() {
        Resource resource  = resourceLoader.getResource(location);
        if(null == resource){
            return  null;
        }
        return loadFileIo(resource);
    }

    private String loadFileIo(Resource resource) {
        try(FileInputStream fis  = new FileInputStream(resource.getFile());
            ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);){
            byte[] b = new byte[1024];
            int len = -1;
            while((len = fis.read(b)) != -1) {
                bos.write(b, 0, len);
            }
            byte[] buffer = bos.toByteArray();
            return new String(buffer, "UTF-8");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
